package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle
import com.haoshuang.zhouzhoubang.MyApp
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityOrderYsBinding
import com.haoshuang.zhouzhoubang.ui.vm.OrderYsViewModel
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.imageResource
import com.mufeng.libs.utils.view.load
import com.mufeng.libs.utils.view.visible


/**
 *
 */
class OrderYsActivity : BaseVMActivity<OrderYsViewModel, ActivityOrderYsBinding>() {
    private var qrcode_url = ""
    private var orderId = ""
    private var finish_status = 0 // 0 未验收, 1: 已验收

    override fun initView(savedInstanceState: Bundle?) {
        qrcode_url = intent.getStringExtra("qrcode_url").toString()
        orderId = intent.getString("orderId")
        finish_status = intent.getIntExtra("finish_status", 0)
        binding.appBar.ivBack.clickWithTrigger { finish() }

        setupData()

        binding.tvFinishTime.text = "邀请扫描下放二维码/也可等待小程序${MyApp.systemConfig?.finish_time}天自动验收"

        binding.tvConfirm.clickWithTrigger {
            if (finish_status == 0){
                // 刷新
                vm.checkOrder("id" to orderId)
            }else{
                finish()
            }
        }

    }

    override fun initData() {

        binding.ivLL.load(qrcode_url)
    }

    private fun setupData(){
        if (finish_status == 0){
            // 未验收
            binding.appBar.tvTitle.text = "等待验收"
            binding.ivIcon.imageResource = R.mipmap.ddy
            binding.tvContent.text = "等待验收，感谢您的真诚付出"
            binding.tvDd.text = "等待客户验收"
            binding.ivLL.visible()
            binding.tvTip.gone()
            binding.tvConfirm.text = "刷新"
        }else{
            // 已验收
            binding.appBar.tvTitle.text = "服务完工"
            binding.ivIcon.imageResource = R.mipmap.icon_service_finish
            binding.tvContent.text = "服务完工，感谢您的真诚付出"
            binding.tvDd.text = "客户已验收"
            binding.ivLL.gone()
            binding.tvTip.visible()
            binding.tvConfirm.text = "确认"
        }
    }

    override fun startObserve() {
        vm.checkOrderData.observeState(this){
            onLoading {showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                finish_status = it?.finish_status?:0
                setupData()
            }
        }
    }

}